<style lang="scss">
@import "./base.scss";
</style>

<template>
  <div class="page-base">
    <z-space class="page-base__article" vertical>
      <h2 style="margin-top: 0">全局变量</h2>
      <ol style="font-size: 16px">
        <li><el-link @click="goTo('i1')">ZY</el-link></li>
        <li><el-link @click="goTo('i2')">ZY_EXT</el-link></li>
      </ol>
      <h3 id="i1">ZY.lodash</h3>
      <p><el-link href="https://lodash.com/" target="_blank">lodash 官网</el-link> </p>
      <h3>ZY.rid</h3>
      <p>生成随机数</p>
      <h3>ZY.awaitTo</h3>
      <p>等待promise的方法</p>
      <vue-code-mirror class="page-base__code" :js="def.awaitToJs"></vue-code-mirror>
      <h3>ZY.JSON5</h3>
      <p><el-link href="https://github.com/json5/json5" target="_blank">json5 官网</el-link></p>
      <p>加强的json编译器</p>
      <el-divider></el-divider>
      <h3>ZY.Time.dayjs</h3>
      <p><el-link href="https://dayjs.gitee.io/zh-CN/" target="_blank">dayjs 官网</el-link></p>
      <h3>ZY.Time.formatDateTime</h3>
      <p><el-link href="https://day.js.org/docs/zh-CN/durations/format#docsNav" target="_blank">format 格式</el-link></p>
      <vue-code-mirror class="page-base__code" :js="def.d1"></vue-code-mirror>
      <h3>ZY.Time.subtract2Date</h3>
      <vue-code-mirror class="page-base__code" :js="def.d2"></vue-code-mirror>
      <el-divider></el-divider>
      <h3 id="i2">ZY_EXT.eval5</h3>
      <vue-code-mirror class="page-base__code" :js="def.e1"></vue-code-mirror>
      <h3>ZY_EXT.Schema</h3>
      <p><el-link href="https://github.com/yiminghe/async-validator" target="_blank">async-validator 官网</el-link></p>
      <vue-code-mirror class="page-base__code" :js="def.s1"></vue-code-mirror>
    </z-space>
  </div>
</template>

<script>
import VueCodeMirror from "@example/components/VueCodeMirror";
import {commonBase} from "@example/views/base/base";

export default {
  components: {VueCodeMirror},
  mixins: [
    commonBase
  ],
  data() {
    return {
    }
  },
  methods: {
  },
  setup() {
    let def = {
      awaitToJs: `let [err, ret] = await ZY.awaitTo(somePromiseFun())`,
      d1: `// ZY.Time.formatDateTime(Date, 'YYYY-MM-DD HH:mm:ss')

ZY.Time.formatDateTime(new Date()) // '2022-03-30 13:09:10'`,
      d2: `ZY.Time.subtract2Date(new Date(), new Date()) // dayjs 对象
function 时间间隔(date1, date2) {
  /**
   *
   * @param date1 {Date|string (Date format)} 结束日期
   * @param date2 {Date|string (Date format)} 开始日期
   * @returns {null|*}
   */
  if (date1 && date2) {
    return ZY.Time.subtract2Date(date1, date2).asHours();
  }
  return null;
}
      `,
      e1: `ZY_EXT(code: string, context: Object) -> any

ZY_EXT.eval5('a', {a: 1}) // 1`,
      s1: `const descriptor = {
  name: {
    type: 'string',
    required: true,
    validator: (rule, value) => value === 'muji',
  },
  age: {
    type: 'number',
    asyncValidator: (rule, value) => {
      return new Promise((resolve, reject) => {
        if (value < 18) {
          reject('too young');  // reject with error message
        } else {
          resolve();
        }
      });
    },
  },
};
const validator = new ZY_EXT.Schema(descriptor);
validator.validate({ name: 'muji' }, (errors, fields) => {
  if (errors) {
    // validation failed, errors is an array of all errors
    // fields is an object keyed by field name with an array of
    // errors per field
    return handleErrors(errors, fields);
  }
  // validation passed
});
      `
    }

    return {
      def
    }
  }
}
</script>
